<?php
/**
 * 文章分类控制器
 * User: mrlifang
 * Date: 17/5/7
 * Time: 上午9:26
 */

namespace Admin\Controller;


use Admin\Model\CategoryModel;
use Frame\Libs\BaseController;
class CategoryController extends BaseController
{
    /*
     * 显示分类列表
     */
    public function index(){
        $Categorys = CategoryModel::getInstance()->fetchAll();
        //调用
        $Categorys = CategoryModel::getInstance()->categoryList($Categorys,0,0);


        $this->smarty->assign("arr",$Categorys);
        $this->smarty->display("Category".DS."index.html");

    }
    /*
     * 添加文章分类界面显示
     */
    public function add(){
        $Categorys = CategoryModel::getInstance()->fetchAll();
        //调用
        $Categorys = CategoryModel::getInstance()->categoryList($Categorys,0,0);
        $this->smarty->assign("arr",$Categorys);
        $this->smarty->display("Category".DS."add.html");
    }

    /*
     * 插入文章分类数据
     */
    public function insert(){
        //获取表单数据
        $data['classname'] = $_POST['classname'];
        $data['orderby'] = $_POST['orderby'];
        $data['pid'] = $_POST['pid'];

        //调用模型类的insert()方法,写入数据
        $obj = CategoryModel::getInstance();

        if($obj->insert($data)){
            $this->jump("文章分类添加成功!","?c=Category");
        }else{
            $this->jump("文章分类添加失败!","?c=Category");
        }

    }

    /*
     * 修改分类数据
     */
    public function edit(){
        //获取地址栏的id参数
        $id = $_GET['id'];
        //根据ID获取一条记录
        $category = CategoryModel::getInstance()->fetchOne("id = $id");

        $Categorys = CategoryModel::getInstance()->fetchAll();
        $Categorys = CategoryModel::getInstance()->categoryList($Categorys,0,0);
        $this->smarty->assign("categorys",$Categorys);
        $this->smarty->assign("arr",$category);
        $this->smarty->display("Category".DS."edit.html");
    }

    /*
     * 更新分类的方法
     */
    public function update(){
        $id = $_POST['id'];
        $data['classname'] = $_POST['classname'];
        $data['orderby'] = $_POST['orderby'];
        $data['pid'] = $_POST['pid'];

        CategoryModel::getInstance()->update($data,$id);
        $this->jump("id = {$id}的记录更新成功","?c=Category");
    }
    /*
     * 删除分类
     */
    public function delete(){
        $id = $_GET['id'];
        CategoryModel::getInstance()->delete($id);
        $this->jump("id = {$id}的记录删除成功","?c=Category");
    }

}